package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Jjzs;
import service.JjzsService;

import java.util.List;

/**
 * 急救知识表控制器
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/jjzs")
public class JjzsController {
    @Autowired
    private JjzsService jjzsService;

    // 根据ID查询急救知识
    @GetMapping("/{id}")
    public Jjzs getJjzsById(@PathVariable Integer id) {
        Jjzs jjzs = jjzsService.findJjzsById(id);
        if (jjzs != null) {
            jjzsService.incrementViewCount(id);
        }
        return jjzs;
    }

    // 查询所有急救知识
    @GetMapping("/all")
    public List<Jjzs> getAllJjzs() {
        return jjzsService.findAllJjzs();
    }

    // 查询热门急救知识
    @GetMapping("/hot/{limit}")
    public List<Jjzs> getHotJjzs(@PathVariable int limit) {
        return jjzsService.findHotJjzs(limit);
    }

    // 搜索急救知识
    @GetMapping("/search/{keyword}")
    public List<Jjzs> searchJjzs(@PathVariable String keyword) {
        return jjzsService.searchJjzs(keyword);
    }

    // 新增急救知识
    @PostMapping("/add")
    public int addJjzs(@RequestBody Jjzs jjzs) {
        return jjzsService.addJjzs(jjzs);
    }

    // 更新急救知识
    @PutMapping("/update")
    public int updateJjzs(@RequestBody Jjzs jjzs) {
        return jjzsService.updateJjzs(jjzs);
    }

    // 删除急救知识
    @DeleteMapping("/{id}")
    public int deleteJjzs(@PathVariable Integer id) {
        return jjzsService.deleteJjzs(id);
    }
}